package com.party.core.service.mall.biz;

import com.party.common.utils.StringUtils;
import com.party.core.model.mall.MallGoodsResources;
import com.party.core.service.mall.IMallGoodsResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Consumer;

/**
 * 商品资源业务
 * Created by yifeng
 *
 * @date 2018/3/28 0028
 * @time 上午 10:35
 */
@Service
public class GoodsResourceBizService {
    @Autowired
    private IMallGoodsResourcesService mallGoodsResourcesService;

    /**
     * 保存商品资源
     *
     * @param goodsId   商品id
     * @param picList   图片集合
     * @param videoList 视频集合
     * @param memberId  用户id
     */
    public void saveResourceBiz(String goodsId, String picList, String videoList, String memberId) {
        List<MallGoodsResources> oldPicList = mallGoodsResourcesService.findByGoodsId(goodsId, MallGoodsResources.TYPE_PIC);
        oldPicList.forEach(new Consumer<MallGoodsResources>() {
            @Override
            public void accept(MallGoodsResources resources) {
                mallGoodsResourcesService.delete(resources.getId());
            }
        });
        // 图片
        if (StringUtils.isNotEmpty(picList)) {
            int sort = 1;
            for (String pic : picList.split(",")) {
                MallGoodsResources resources = mallGoodsResourcesService.getUnique(new MallGoodsResources(pic, goodsId, MallGoodsResources.TYPE_PIC));
                if (resources == null) {
                    this.saveResource(memberId, goodsId, pic, MallGoodsResources.TYPE_PIC, sort);
                    sort++;
                }
            }
        }

        List<MallGoodsResources> oldVideoList = mallGoodsResourcesService.findByGoodsId(goodsId, MallGoodsResources.TYPE_VIDEO);
        oldVideoList.forEach(new Consumer<MallGoodsResources>() {
            @Override
            public void accept(MallGoodsResources resources) {
                mallGoodsResourcesService.delete(resources.getId());
            }
        });

        // 视频
        if (StringUtils.isNotEmpty(videoList)) {
            String videoUrl = videoList.split(",")[0];
            MallGoodsResources resources = mallGoodsResourcesService.getUnique(new MallGoodsResources(videoUrl, goodsId, MallGoodsResources.TYPE_VIDEO));
            if (resources == null) {
                this.saveResource(memberId, goodsId, videoUrl, MallGoodsResources.TYPE_VIDEO, 1);
            }
        }
    }

    private void saveResource(String memberId, String goodsId, String url, int type, int sort) {
        MallGoodsResources resources = new MallGoodsResources();
        resources.setMemberId(memberId);
        resources.setGoodsId(goodsId);
        resources.setSort(sort);
        resources.setType(type);
        resources.setUrl(url);
        mallGoodsResourcesService.insert(resources);
    }

    /**
     * 获取第一张图
     *
     * @param picList 图片集合
     * @return
     */
    public String getFirstPic(String picList) {
        if (StringUtils.isNotEmpty(picList)) {
            String[] pics = picList.split(",");
            if (pics.length > 0) {
                return pics[0];
            }
        }
        return null;
    }

    /**
     * 复制资源
     *
     * @param oldGoodsId 旧的商品
     * @param newGoodsId 新的商品
     */
    public void copyResource(String oldGoodsId, String newGoodsId) {
        // 图片
        List<MallGoodsResources> picList = mallGoodsResourcesService.findByGoodsId(oldGoodsId, MallGoodsResources.TYPE_PIC);
        for (MallGoodsResources resources : picList) {
            resources.setId(null);
            resources.setCreateDate(null);
            resources.setUpdateDate(null);
            resources.setGoodsId(newGoodsId);
            mallGoodsResourcesService.insert(resources);
        }
        // 视频
        List<MallGoodsResources> videoList = mallGoodsResourcesService.findByGoodsId(oldGoodsId, MallGoodsResources.TYPE_VIDEO);
        for (MallGoodsResources resources : videoList) {
            resources.setId(null);
            resources.setCreateDate(null);
            resources.setUpdateDate(null);
            resources.setGoodsId(newGoodsId);
            mallGoodsResourcesService.insert(resources);
        }
    }

    public void deleteByGoods(String goodsId) {
        mallGoodsResourcesService.deleteByGoodsId(goodsId);
    }
}
